
import { login } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    info: JSON.parse(localStorage.getItem('info')) || {}
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    setInfo(state, newInfo) {
      state.info = newInfo
      localStorage.setItem('info', JSON.stringify(newInfo))
    }
  },
  actions: {
    async login(context, data) {
      // console.log(context, data)
      const res = await login(data)
      console.log(res)
      const token = res.data.token.token
      context.commit('setToken', token)
      context.commit('setInfo', res.data.user)
    }
  }
}
